package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.CrossActivityMallDayBuyData;

import java.math.BigInteger;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossActivityMallDayBuyDaoImpl extends TempDataObjectDao<CrossActivityMallDayBuyData> {

    public static String getCrossActivityMallDayBuyDataSql = "select * from `t_c_activity_mall_day_buy` where `activityId`=?";
    public List<CrossActivityMallDayBuyData> getCrossActivityMallDayBuyDataList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossActivityMallDayBuyData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossActivityMallDayBuyDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossActivityMallDayBuyData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateCrossActivityMallDayBuyDataSql = "update `t_c_activity_mall_day_buy` set buyCount=?,fakeBuyCount=?,serverBuyCountMap=?,serverIdList=?,status=?,lastAddFakeTime=? where activityId=? and mallId=? and day=?";
    @Override
    protected boolean updateData(CrossActivityMallDayBuyData crossActivityMallDayBuyData) {
        crossActivityMallDayBuyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossActivityMallDayBuyData.getBuyCount()));
        para.put(i++, new DbParameter(Types.BIGINT, crossActivityMallDayBuyData.getFakeBuyCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossActivityMallDayBuyData.getServerBuyCountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossActivityMallDayBuyData.getServerIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossActivityMallDayBuyData.getStatus()));
        para.put(i++, new DbParameter(Types.BIGINT, crossActivityMallDayBuyData.getLastAddFakeTime()));

        para.put(i++, new DbParameter(Types.INTEGER, crossActivityMallDayBuyData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossActivityMallDayBuyData.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossActivityMallDayBuyData.getDay()));
        if (getDb().executeNoneQuery(updateCrossActivityMallDayBuyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossActivityMallDayBuyDataSql = "insert into `t_c_activity_mall_day_buy`(activityId,mallId,day,buyCount,fakeBuyCount,serverBuyCountMap,serverIdList,status,lastAddFakeTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossActivityMallDayBuyData crossActivityMallDayBuyData) {
        crossActivityMallDayBuyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossActivityMallDayBuyData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossActivityMallDayBuyData.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossActivityMallDayBuyData.getDay()));
        para.put(i++, new DbParameter(Types.BIGINT, crossActivityMallDayBuyData.getBuyCount()));
        para.put(i++, new DbParameter(Types.BIGINT, crossActivityMallDayBuyData.getFakeBuyCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossActivityMallDayBuyData.getServerBuyCountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossActivityMallDayBuyData.getServerIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossActivityMallDayBuyData.getStatus()));
        para.put(i++, new DbParameter(Types.BIGINT, crossActivityMallDayBuyData.getLastAddFakeTime()));
        if (getDb().executeNoneQuery(insertCrossActivityMallDayBuyDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossActivityMallDayBuyData parseData(ResultSet rs) throws SQLException {
        CrossActivityMallDayBuyData crossActivityMallDayBuyData = new CrossActivityMallDayBuyData();
        crossActivityMallDayBuyData.setActivityId(rs.getInt("activityId"));
        crossActivityMallDayBuyData.setMallId(rs.getInt("mallId"));
        crossActivityMallDayBuyData.setDay(rs.getInt("day"));
        crossActivityMallDayBuyData.setBuyCount(rs.getLong("buyCount"));
        crossActivityMallDayBuyData.setFakeBuyCount(rs.getLong("fakeBuyCount"));
        crossActivityMallDayBuyData.setServerBuyCountMap(JSON.parseObject(rs.getString("serverBuyCountMap"), new TypeReference<java.util.Map<java.lang.Long, java.lang.Long>>() {}));
        crossActivityMallDayBuyData.setServerIdList(JSON.parseObject(rs.getString("serverIdList"), new TypeReference<java.util.List<java.lang.Long>>() {}));
        crossActivityMallDayBuyData.setStatus(rs.getInt("status"));
        crossActivityMallDayBuyData.setLastAddFakeTime(rs.getLong("lastAddFakeTime"));

        return crossActivityMallDayBuyData;
    }

}
